/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codetemplate.htmlunit.test;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.bcel.generic.DDIV;

/**
 *正则表达式的常用方法
 * @author itmagus
 */
public class ZzUtil {
    private static final Logger log=Logger.getLogger(ZzUtil.class.getName());
    /**
     * 解析自己定义的表达式
     * @param regx 自已定义的表达式({@*}、{@title}、{@url}、{@post_time})等,表达工中只能含字母、数字、下划线、星号
     * @return 按循序返回*、title、url等字段的LIST集合
     */
    public static List<String> getBdsList(String regx){
    String reg="\\{[@|#]([*!a-z_A-Z0-9]*)\\}";
           Pattern pattern = Pattern.compile(reg); 
        List<String> fileds=new ArrayList<String>();
          Matcher m= pattern.matcher(regx);
           while(m.find()){
               fileds.add(m.group(1));
           }
           return fileds;
    }
        /**
     * 解析自己定义的表达式
     * @param regx 自已定义的表达式({@*}、{@title}、{@url}、{@post_time})等,表达工中只能含字母、数字、下划线、星号
     * @return 按循序返回title、url等字段的LIST集合,不包括*号
     */
    public static List<String> getBdsListWithoutFlag(String regx){
         String reg="\\{[@|#]([*!a-z_A-Z0-9]*)\\}";
       //String reg="\\{@([*a-z_A-Z0-9]*)\\}";
        Pattern pattern = Pattern.compile(reg); 
        List<String> fileds=new ArrayList<String>();
        Matcher m= pattern.matcher(regx);
        while(m.find()){
            if( m.group(1).equals("*") || m.group(1).equals("!")){
                continue ;
            }
            fileds.add(m.group(1));
        }
        return fileds;
    }
    
    /**
     * 表达式转换，将自己定义的表达式，转换成正则表达式
     * @param regx 自已定义的表达式({@*}、{@title}、{@url}、{@post_time})等,表达工中只能含字母、数字、下划线、星号
     *  {@!}表示空格、换行
     *  
     * @return 正则表达式
     */
    public static String replaceRegx(String regx){
    return regx.replaceAll("\\{@[*a-z_A-Z0-9]*\\}","([\\\\s\\\\S]*?)").replaceAll("\\{@[!]\\}", "([\\\\s]*)")
            .replaceAll("\\{#[*a-z_A-Z0-9]*\\}","([\\\\S]*)");//([\\\\s\\\\S]\\*\\?)
    }
    /**
     * 提取HTML内容
     * @param content 要解析的HTML内容
     * @param regx 自已定义的表达式({@*}、{@title}、{@url}、{@post_time})等,表达工中只能含字母、数字、下划线、星号
     * @return 返回提取出来的内容{@*}除外，格式为List<Map<String,String>>
     */
    public static List<Map<String,String>> getData(String content,String regx){
         List<String> files=getBdsList(regx);
         String endString=replaceRegxEnd(regx);
         regx=replaceRegx(regx);
         return getData(content, regx, files,endString);
    }
    
     public static String replaceRegx(String regx,int length){
    return regx.replaceAll("\\{@[*a-z_A-Z0-9]*\\}","([\\\\s\\\\S]{0,"+length+"}?)").replaceAll("\\{@[!]\\}", "([\\\\s]{0,"+length+"}?)");//([\\\\s\\\\S]\\*\\?)
    }
     
      public static String replaceRegxEnd(String regx){
    return regx.replaceAll("\\{[@|#][*a-z_A-Z0-9]*\\}","a").replaceAll("\\{@[!]\\}", "\t");//([\\\\s\\\\S]\\*\\?)
    }
    
   
    
    public static List<Map<String,String>> getData(String content,String regx,int length){
         List<String> files=getBdsList(regx);
         String endString=replaceRegxEnd(regx);
         regx=replaceRegx(regx,length);
         return getData(content, regx, files,endString);
    }
    
    /**
     * 提取HTML内容
     * @param content 要解析的HTML内容
     * @param zz_regx 定义的模板正则表达式
     * @param fileds 表达式中字段的集合
     * @return 返回提取出来的内容{@*}除外，格式为List<Map<String,String>>
     */
    
    public static List<Map<String,String>> getData(String content,String zz_regx,List<String> fileds,String endString){
        List<Map<String,String>> datalist=new ArrayList<Map<String, String>>();
        try {
            Pattern p = Pattern.compile(zz_regx);  
            Matcher m1 = p.matcher(content+endString);  
            while(m1.find()){
                    if(m1.group(0).indexOf(endString)!=-1){
//                        log.info("正则提取内容结束.....");
                        break;
                    }
                Map<String,String> data=new HashMap<String, String>();
                for(int i=1;i<=m1.groupCount();i++){
                    String filed=fileds.get(i-1);
                    if(filed.equals("*")||filed.equals("!"))
                        continue;
                    data.put(filed, m1.group(i).trim());
                }
                datalist.add(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datalist;
    }
    
    public static void main( String[] args ) throws IOException, InterruptedException, ExecutionException{
        Long o1 = System.currentTimeMillis();
        WebClient web = HTest.getWebclient();
        for (int u = 0; u < 1; u++) {
            try {
                web.setJavaScriptEnabled(false);
//                web.setJavaScriptTimeout(10000);
                String url = "http://bbs.city.tianya.cn/tianyacity/content/191/1/540545.shtml";
                HtmlPage page = web.getPage( url );
                page.cleanUp();
                String gg = page.asXml() ;
//                System.out.println( gg );
                
                
//                int a = 0xc2 ;
//                int b = 0xa0;
//                space[0] = 0xc2;
//                string UTFSpace = Encoding.GetEncoding("UTF-8").GetString(space);
//                HtmlStr = HtmlStr.Replace(UTFSpace,"&nbsp;");
                        

            	System.out.println( "循环"+u );
//                String regex = "<title>{@title}</title>{@*}<div class=\"info\">{@*}：{@click_num} {@*}：{@re_num}</div>{@*}作者：{@username}<span name={@*}</span>{@post_time}</tr>{@*}<div class=\"post\"{@*}>{@text}<div class=\"post-jb\">";
                String regex = "<title>{@title}</title>{@*}<div class=\"info\">{@*}访问数：{@click_num}回复数：{@re_num}</div>{@*}<span class=\"floor\">{@*}作者：{@username}<span name={@*}</span>{@*}回复日期：{@post_time}</div>{@*}<div class=\"post\"{@*}>{@text}<div class=\"post-jb\">";
                List<Map<String,String>> datalist=ZzUtil.getData( page.asXml(), regex);
                System.out.println(datalist.size());
                for (int i = 0; i < datalist.size(); i++) {
                    Iterator<Map.Entry<String, String>> it = datalist.get(i).entrySet().iterator();
                    while( it.hasNext() ){
                        Map.Entry<String, String> en = it.next();
        //                en.setValue( URLUtil.htmlToText(en.getValue()).trim() );
                        String s = en.getValue();
                        s = s.replaceAll( "(^(\\s|　)*)|((\\s|　)*$)", "" );
                        if( en.getKey().equals("click_num")){
//                        	int ss = Integer.parseInt(s);
                        	char[]chars=s.toCharArray(); 
                        	for(int ik=0; ik< chars.length ;ik++){ 
                        		System.out.println(" "+chars[ik]+" "+(int)chars[ik]);
                            } 
//                        	System.out.println("转换数字="+ss);
                        }
                        en.setValue(s);
                        System.out.println( en.getKey()+":"+en.getValue().trim() );
                    }
                    System.out.println( "-------------------------" );
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
//                web.closeAllWindows();
            }
        }
        Long o2 = System.currentTimeMillis();
         web.closeAllWindows();
        System.out.println( (o2 - o1)/1000000 );
        
        
//        String xpath = "//a[text()='预览']";
//        List<?> byXPath = page.getByXPath( xpath );
//        if(byXPath.size()>0){
//            HtmlElement d = (HtmlElement)byXPath.get(0);
//            page = d.click();
//            System.out.println( page.asXml() );
//        }
        
        
//        dd();
//        System.out.println( "主线程休眠" );
//        System.gc();
//        Thread.sleep( 1000 * 60 * 1 );
//        System.gc();
//        Thread.sleep( 1000 * 60 * 1 );
//        System.gc();
//        Thread.sleep( 1000 * 60 * 1 );
//        System.gc();
//        Thread.sleep( 1000 * 60 * 15 );
    }
    
    private static void dd() throws InterruptedException, ExecutionException{
        List<FutureTask> ftlist = new ArrayList<FutureTask>();
        for (int i = 0; i < 10; i++) {
            FutureTask ft = new FutureTask( new Ttesttask() );
            ftlist.add(ft);
            new Thread( ft ).start();
        }
        
        for (int i = 0; i < ftlist.size(); i++) {
            FutureTask ft = ftlist.get(i);
            ft.get();
        }
    }
}

class Ttesttask implements Callable<Object>{
    public Object call() throws Exception {
        WebClient web = HTest.getWebclient();
        try {
            web.setJavaScriptEnabled(true);
            web.setJavaScriptTimeout(30000);
            String url = "http://news.163.com/12/0819/11/8994BHL00001124J.html";
            HtmlPage page = web.getPage( url );
            page.cleanUp();
    //            System.out.println( page.asXml() );
                System.out.println( "执行" );
            String regex = "<span{@*}class=\"s2\"{@*}>(?:<em{@*}>)?{@num1}(?:</em>)?{@*}/{@num2}</span>";
            List<Map<String,String>> datalist=ZzUtil.getData( page.asXml(), regex);
            System.out.println(datalist.size());
            for (int i = 0; i < datalist.size(); i++) {
                Iterator<Map.Entry<String, String>> it = datalist.get(i).entrySet().iterator();
                while( it.hasNext() ){
                    Map.Entry<String, String> en = it.next();
    //                en.setValue( URLUtil.htmlToText(en.getValue()).trim() );
                    System.out.println( en.getKey()+":"+en.getValue() );
                }
                System.out.println( "-------------------------" );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            web.closeAllWindows();
            web = null;
        }
        return null ;
    }
}